<?php

namespace App\Http\Controllers\Api;

use App\Models\Book;
use App\Models\BookComment;
use App\Models\BookStockRemindUser;
use App\Models\Cart;
use App\Models\Category;
use App\Models\User;
use App\Models\UserSearchKeyword;
use Illuminate\Http\Request;

class BookController extends BaseController
{
    public function index(Request $request)
    {
        $page         = intval($request->get('page',1));
        $page         = max($page ,1);
        $page_size    = intval($request->get('page_size',10));
        $category_pid = intval($request->get('category_pid'));  //一级分类id
        $category_id  = intval($request->get('category_id'));  //0全部
        $order        = intval($request->get('order',1));  //1综合排序 2评分优先 3折扣优先 4低价优先

        if(empty($category_pid) || empty($order) || !in_array($order,[1,2,3,4]))
        {
            return $this->err(1,200,'','参数错误');
        }

        // 创建一个查询构造器
        $columns = ['id','title','image','category_pid','category_ids','score','total_stock','author','old_price','medium_book_price','medium_book_price2','medium_book_price3','medium_book_price4','medium_book_price5','medium_book_price6','good_book_price','new_book_price','new_book_stock','good_book_stock','medium_book_stock','medium_book_stock2','medium_book_stock3','medium_book_stock4','medium_book_stock5','medium_book_stock6','medium_book_discount','new_book_discount','good_book_discount','medium_book_discount2','medium_book_discount3','medium_book_discount4','medium_book_discount5','medium_book_discount6'];
        $builder = Book::query()->select($columns)->state()->undeleted()->where('category_pid',$category_pid);

        if(!empty($category_id))
        {
            $builder->where(function ($query) use ($category_id) {
                $query->whereRaw("FIND_IN_SET($category_id,category_ids)");
            });
        }

        switch ($order)
        {
            case 1:
                $builder->orderBy('total_stock','desc');
                break;
            case 2:
                $builder->orderBy('score','desc');
                break;
            case 3:
                $builder->orderBy('medium_book_discount','asc');
                break;
            case 4:
                $builder->orderBy('medium_book_price','asc');
                break;
            default:
                $builder->orderBy('total_stock','desc');
                break;
        }

        $list = $builder->paginate($page_size,$columns,'page',$page);
        $items = [];
        if(!empty($list->items()))
        {
            foreach ($list->items() as $item)
            {
                $stock_arr = [];
                $discount_arr = [];
                $price_arr = [];

                $discount_arr2 = [];
                $price_arr2 = [];
                if($item->new_book_stock > 0)
                {
                    $stock_arr[] = $item->new_book_stock;
                    $discount_arr2[] = $item->new_book_discount;
                    $price_arr2[] = $item->new_book_price;
                }
                else
                {
                    $discount_arr[] = $item->new_book_discount;
                    $price_arr[] = $item->new_book_price;
                }

                if($item->good_book_stock > 0)
                {
                    $stock_arr[] = $item->good_book_stock;
                    $discount_arr2[] = $item->good_book_discount;
                    $price_arr2[] = $item->good_book_price;
                }
                else
                {
                    $discount_arr[] = $item->good_book_discount;
                    $price_arr[] = $item->good_book_price;
                }

                if($item->medium_book_stock > 0)
                {
                    $stock_arr[] = $item->medium_book_stock;
                    $discount_arr2[] = $item->medium_book_discount;
                    $price_arr2[] = $item->medium_book_price;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount;
                    $price_arr[] = $item->medium_book_price;
                }

                if($item->medium_book_stock2 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock2;
                    $discount_arr2[] = $item->medium_book_discount2;
                    $price_arr2[] = $item->medium_book_price2;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount2;
                    $price_arr[] = $item->medium_book_price2;
                }

                if($item->medium_book_stock3 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock3;
                    $discount_arr2[] = $item->medium_book_discount3;
                    $price_arr2[] = $item->medium_book_price3;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount3;
                    $price_arr[] = $item->medium_book_price3;
                }

                if($item->medium_book_stock4 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock4;
                    $discount_arr2[] = $item->medium_book_discount4;
                    $price_arr2[] = $item->medium_book_price4;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount4;
                    $price_arr[] = $item->medium_book_price4;
                }

                if($item->medium_book_stock5 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock5;
                    $discount_arr2[] = $item->medium_book_discount5;
                    $price_arr2[] = $item->medium_book_price5;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount5;
                    $price_arr[] = $item->medium_book_price5;
                }

                if($item->medium_book_stock6 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock6;
                    $discount_arr2[] = $item->medium_book_discount6;
                    $price_arr2[] = $item->medium_book_price6;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount6;
                    $price_arr[] = $item->medium_book_price6;
                }

                $min_discount = 0;
                $min_price = 0.0;
                $is_more = 0;
                if(count($stock_arr) > 1)
                {
                    $is_more = 1;
                }

                if(count($stock_arr) == 0)
                {
                    if(!empty($discount_arr))
                    {
                        $min_discount = min($discount_arr);
                    }
                    if(!empty($price_arr))
                    {
                        $min_price = min($price_arr);
                    }
                }
                else
                {
                    if(!empty($discount_arr2))
                    {
                        $min_discount = min($discount_arr2);
                    }
                    if(!empty($price_arr2))
                    {
                        $min_price = min($price_arr2);
                    }
                }


                $items[] = [
                    'id'                             => $item->id,
                    'title'                          => $item->title,
                    'author'                         => $item->author,
                    'image'                          => $item->image,
                    'category_pid'                   => $item->category_pid,
                    'category_ids'                   => $item->category_ids,
                    'score'                          => $item->score,
                    'total_stock'                    => $item->total_stock,
                    'new_book_price'                 => $item->new_book_price,
                    'new_book_stock'                 => $item->new_book_stock,
                    'good_book_price'                => $item->good_book_price,
                    'good_book_stock'                => $item->good_book_stock,
                    'medium_book_price'              => $item->medium_book_price,
                    'medium_book_stock'              => $item->medium_book_stock,
                    'medium_book_price2'             => $item->medium_book_price2,
                    'medium_book_stock2'             => $item->medium_book_stock2,
                    'medium_book_price3'             => $item->medium_book_price3,
                    'medium_book_stock3'             => $item->medium_book_stock3,
                    'medium_book_price4'             => $item->medium_book_price4,
                    'medium_book_stock4'             => $item->medium_book_stock4,
                    'medium_book_price5'             => $item->medium_book_price5,
                    'medium_book_stock5'             => $item->medium_book_stock5,
                    'medium_book_price6'             => $item->medium_book_price6,
                    'medium_book_stock6'             => $item->medium_book_stock6,
                    'new_book_discount'              => $item->new_book_discount,
                    'good_book_discount'             => $item->good_book_discount,
                    'medium_book_discount'           => $item->medium_book_discount,
                    'medium_book_discount2'          => $item->medium_book_discount2,
                    'medium_book_discount3'          => $item->medium_book_discount3,
                    'medium_book_discount4'          => $item->medium_book_discount4,
                    'medium_book_discount5'          => $item->medium_book_discount5,
                    'medium_book_discount6'          => $item->medium_book_discount6,
                    'old_price'                      => $item->old_price,
                    'is_more'                        => $is_more,
                    'min_discount'                   => $min_discount,
                    'min_price'                      => $min_price,
                ];
            }
        }

        $data = [
            '_meta' => [
                'total_count'  => $list->total(),
                'page_count'   => $list->lastPage(),
                'current_page' => $list->currentPage(),
                'per_page'     => $list->perPage()
            ],
            "items" => $items
        ];

        return $this->res([
            'data' => $data
        ]);
    }

    public function search(Request $request)
    {
        $keyword = trim($request->get('keyword'));


        $keyword = preg_replace('/\[(.*?)\]/', '', $keyword);
        //echo $keyword;die;
        $result['stock'] = [];
        $result['unstock'] = [];

        if(!empty($keyword))
        {
            // 创建一个查询构造器
            $columns = ['*'];
            $builder = Book::query()->select($columns)->state()->undeleted();

            $like = '%'.$keyword.'%';
            $builder->where(function ($query) use ($like) {
                $query->where('title', 'like', $like)
                    ->orWhere('author', 'like', $like);
            });

            $list = $builder->orderBy('id','desc')->get();
            if(!empty($list))
            {
                foreach ($list as $v)
                {
                    $stock_arr = [];
                    $discount_arr = [];
                    $price_arr = [];

                    $discount_arr2 = [];
                    $price_arr2 = [];
                    if($v->new_book_stock > 0)
                    {
                        $stock_arr[] = $v->new_book_stock;
                        $discount_arr2[] = $v->new_book_discount;
                        $price_arr2[] = $v->new_book_price;
                    }
                    else
                    {
                        $discount_arr[] = $v->new_book_discount;
                        $price_arr[] = $v->new_book_price;
                    }

                    if($v->good_book_stock > 0)
                    {
                        $stock_arr[] = $v->good_book_stock;
                        $discount_arr2[] = $v->good_book_discount;
                        $price_arr2[] = $v->good_book_price;
                    }
                    else
                    {
                        $discount_arr[] = $v->good_book_discount;
                        $price_arr[] = $v->good_book_price;
                    }

                    if($v->medium_book_stock > 0)
                    {
                        $stock_arr[] = $v->medium_book_stock;
                        $discount_arr2[] = $v->medium_book_discount;
                        $price_arr2[] = $v->medium_book_price;
                    }
                    else
                    {
                        $discount_arr[] = $v->medium_book_discount;
                        $price_arr[] = $v->medium_book_price;
                    }

                    if($v->medium_book_stock2 > 0)
                    {
                        $stock_arr[] = $v->medium_book_stock2;
                        $discount_arr2[] = $v->medium_book_discount2;
                        $price_arr2[] = $v->medium_book_price2;
                    }
                    else
                    {
                        $discount_arr[] = $v->medium_book_discount2;
                        $price_arr[] = $v->medium_book_price2;
                    }

                    if($v->medium_book_stock3 > 0)
                    {
                        $stock_arr[] = $v->medium_book_stock3;
                        $discount_arr2[] = $v->medium_book_discount3;
                        $price_arr2[] = $v->medium_book_price3;
                    }
                    else
                    {
                        $discount_arr[] = $v->medium_book_discount3;
                        $price_arr[] = $v->medium_book_price3;
                    }

                    if($v->medium_book_stock4 > 0)
                    {
                        $stock_arr[] = $v->medium_book_stock4;
                        $discount_arr2[] = $v->medium_book_discount4;
                        $price_arr2[] = $v->medium_book_price4;
                    }
                    else
                    {
                        $discount_arr[] = $v->medium_book_discount4;
                        $price_arr[] = $v->medium_book_price4;
                    }

                    if($v->medium_book_stock5 > 0)
                    {
                        $stock_arr[] = $v->medium_book_stock5;
                        $discount_arr2[] = $v->medium_book_discount5;
                        $price_arr2[] = $v->medium_book_price5;
                    }
                    else
                    {
                        $discount_arr[] = $v->medium_book_discount5;
                        $price_arr[] = $v->medium_book_price5;
                    }

                    if($v->medium_book_stock6 > 0)
                    {
                        $stock_arr[] = $v->medium_book_stock6;
                        $discount_arr2[] = $v->medium_book_discount6;
                        $price_arr2[] = $v->medium_book_price6;
                    }
                    else
                    {
                        $discount_arr[] = $v->medium_book_discount6;
                        $price_arr[] = $v->medium_book_price6;
                    }

                    $is_more = 0;
                    if(count($stock_arr) > 1)
                    {
                        $is_more = 1;
                    }

                    $min_discount = 0;
                    $min_price = 0.0;

                    if(count($stock_arr) == 0)
                    {
                        if(!empty($discount_arr))
                        {
                            $min_discount = min($discount_arr);
                        }

                        if(!empty($price_arr))
                        {
                            $min_price = min($price_arr);
                        }
                    }
                    else
                    {
                        if(!empty($discount_arr2))
                        {
                            $min_discount = min($discount_arr2);
                        }

                        if(!empty($price_arr2))
                        {
                            $min_price = min($price_arr2);
                        }
                    }


                    $item = [
                        'id'                             => $v->id,
                        'title'                          => $v->title,
                        'image'                          => $v->image,
                        'score'                          => $v->score,
                        'total_stock'                    => $v->total_stock,
                        'author'                         => $v->author,
                        'medium_book_price'              => $v->medium_book_price,
                        'medium_book_stock'              => $v->medium_book_stock,
                        'new_book_price'                 => $v->new_book_price,
                        'new_book_stock'                 => $v->new_book_stock,
                        'good_book_price'                => $v->good_book_price,
                        'good_book_stock'                => $v->good_book_stock,
                        'medium_book_price2'             => $v->medium_book_price2,
                        'medium_book_stock2'             => $v->medium_book_stock2,
                        'medium_book_price3'             => $v->medium_book_price3,
                        'medium_book_stock3'             => $v->medium_book_stock3,
                        'medium_book_price4'             => $v->medium_book_price4,
                        'medium_book_stock4'             => $v->medium_book_stock4,
                        'medium_book_price5'             => $v->medium_book_price5,
                        'medium_book_stock5'             => $v->medium_book_stock5,
                        'medium_book_price6'             => $v->medium_book_price6,
                        'medium_book_stock6'             => $v->medium_book_stock6,
                        'new_book_discount'              => $v->new_book_discount,
                        'good_book_discount'             => $v->good_book_discount,
                        'medium_book_discount'           => $v->medium_book_discount,
                        'medium_book_discount2'          => $v->medium_book_discount2,
                        'medium_book_discount3'          => $v->medium_book_discount3,
                        'medium_book_discount4'          => $v->medium_book_discount4,
                        'medium_book_discount5'          => $v->medium_book_discount5,
                        'medium_book_discount6'          => $v->medium_book_discount6,
                        'is_more'                        => $is_more,
                        'min_discount'                   => $min_discount,
                        'min_price'                      => $min_price,
                    ];

                    if($v->total_stock > 0)
                    {
                        $result['stock'][] = $item;
                    }
                    else
                    {
                        //判断是否已经设置到货提醒
                        $row = \DB::table('book_stock_remind_users')->where('user_id',$this->user_id)->where('book_id',$v->id)->first();

                        $is_setting_remind = 0;
                        if($row)
                        {
                            $is_setting_remind = 1;
                        }
                        $item['is_setting_remind'] = $is_setting_remind;
                        $result['unstock'][] = $item;
                    }

                }

                $row = UserSearchKeyword::query()->where('user_id',$this->user_id)->where('keyword',$keyword)->first();
                if(empty($row))
                {
                    UserSearchKeyword::create([
                        'user_id'       => $this->user_id,
                        'keyword'       => $keyword,
                        'created_at'    => $this->time,
                    ]);
                }
            }
        }

        return $this->ok($result);
    }

    public function categories(Request $request)
    {
        $pid = intval($request->get('pid'));  //一级分类id
        if(empty($pid))
        {
            return $this->err(1,200,'','参数错误');
        }


        $items[] = ['id' => 0, 'title' => '全部', 'sort' => 0];
        $list = Category::query()->select(['id','title','sort'])->where('pid',$pid)->orderBy('sort','asc')->get();
        if(!empty($list))
        {
            foreach ($list as $v)
            {
                $items[] = [
                    'id'    => $v->id,
                    'title' => $v->title,
                    'sort'  => $v->sort,
                ];
            }
        }

        return $this->ok($items);
    }

    public function show(Request $request)
    {
        $book_id = intval($request->get('book_id'));
        if(empty($book_id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $book = $this->_check_info($book_id);
        if(empty($book))
        {
            return $this->err(1,200,'','图书不存在');
        }


        $is_setting_remind = 0;
        $row = BookStockRemindUser::query()->where('user_id',$this->user_id)->where('book_id',$book_id)->first();
        if(!empty($row))
        {
            $is_setting_remind = 1;
        }

        $book['is_setting_remind'] = $is_setting_remind;

        $cart = Cart::query()->where('book_id',$book_id)->where('user_id',$this->user_id)->first();
        $book['cart'] = $cart ? $cart : (object)null;

        //判断是否有多个品相
        $stock_arr = [];
        $discount_arr = [];
        $price_arr = [];

        $discount_arr2 = [];
        $price_arr2 = [];
        if($book['new_book_stock'] > 0)
        {
            $stock_arr[] = $book['new_book_stock'];
            $discount_arr2[] = $book['new_book_discount'];
            $price_arr2[] = $book['new_book_price'];
        }
        else
        {
            $discount_arr[] = $book['new_book_discount'];
            $price_arr[] = $book['new_book_price'];
        }

        if($book['good_book_stock'] > 0)
        {
            $stock_arr[] = $book['good_book_stock'];
            $discount_arr2[] = $book['good_book_discount'];
            $price_arr2[] = $book['good_book_price'];
        }
        else
        {
            $discount_arr[] = $book['good_book_discount'];
            $price_arr[] = $book['good_book_price'];
        }

        if($book['medium_book_stock'] > 0)
        {
            $stock_arr[] = $book['medium_book_stock'];
            $discount_arr2[] = $book['medium_book_discount'];
            $price_arr2[] = $book['medium_book_price'];
        }
        else
        {
            $discount_arr[] = $book['medium_book_discount'];
            $price_arr[] = $book['medium_book_price'];
        }

        if($book['medium_book_stock2'] > 0)
        {
            $stock_arr[] = $book['medium_book_stock2'];
            $discount_arr2[] = $book['medium_book_discount2'];
            $price_arr2[] = $book['medium_book_price2'];
        }
        else
        {
            $discount_arr[] = $book['medium_book_discount2'];
            $price_arr[] = $book['medium_book_price2'];
        }

        if($book['medium_book_stock3'] > 0)
        {
            $stock_arr[] = $book['medium_book_stock3'];
            $discount_arr2[] = $book['medium_book_discount3'];
            $price_arr2[] = $book['medium_book_price3'];
        }
        else
        {
            $discount_arr[] = $book['medium_book_discount3'];
            $price_arr[] = $book['medium_book_price3'];
        }

        if($book['medium_book_stock4'] > 0)
        {
            $stock_arr[] = $book['medium_book_stock4'];
            $discount_arr2[] = $book['medium_book_discount4'];
            $price_arr2[] = $book['medium_book_price4'];
        }
        else
        {
            $discount_arr[] = $book['medium_book_discount4'];
            $price_arr[] = $book['medium_book_price4'];
        }

        if($book['medium_book_stock5'] > 0)
        {
            $stock_arr[] = $book['medium_book_stock5'];
            $discount_arr2[] = $book['medium_book_discount5'];
            $price_arr2[] = $book['medium_book_price5'];
        }
        else
        {
            $discount_arr[] = $book['medium_book_discount5'];
            $price_arr[] = $book['medium_book_price5'];
        }

        if($book['medium_book_stock6'] > 0)
        {
            $stock_arr[] = $book['medium_book_stock6'];
            $discount_arr2[] = $book['medium_book_discount6'];
            $price_arr2[] = $book['medium_book_price6'];
        }
        else
        {
            $discount_arr[] = $book['medium_book_discount6'];
            $price_arr[] = $book['medium_book_price6'];
        }

        $is_more = 0;
        if(count($stock_arr) > 1)
        {
            $is_more = 1;
        }

        $min_discount = 0;
        $min_price = 0.0;

        if(count($stock_arr) == 0)
        {
            if(!empty($discount_arr))
            {
                $min_discount = min($discount_arr);
            }

            if(!empty($price_arr))
            {
                $min_price = min($price_arr);
            }
        }
        else
        {
            if(!empty($discount_arr2))
            {
                $min_discount = min($discount_arr2);
            }

            if(!empty($price_arr2))
            {
                $min_price = min($price_arr2);
            }
        }


        $book['is_more'] = $is_more;
        $book['min_discount'] = $min_discount;
        $book['min_price'] = $min_price;
        return $this->ok($book);
    }

    public function relations(Request $request)
    {
        $book_id = intval($request->get('book_id'));
        if(empty($book_id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $book = $this->_check_info($book_id);
        if(empty($book))
        {
            return $this->err(1,200,'','图书不存在');
        }

        $books = [];
        if(!empty($book['category_ids']))
        {
            $category_ids_arr = explode(',',$book['category_ids']);
            $book_relations = \DB::table('book_category_relations')->whereIn('category_id',$category_ids_arr)->get();

            $book_ids_arr = [];
            if(!empty($book_relations))
            {
                foreach ($book_relations as $v)
                {
                    if(!in_array($v->book_id,$book_ids_arr))
                    {
                        $book_ids_arr[] = $v->book_id;
                    }

                }
            }


            if(!empty($book_ids_arr))
            {
                $count = Book::query()->whereIn('id',$book_ids_arr)->state()->undeleted()->stock()->count();
                if($count > 60)
                {
                    $books = Book::query()->select(['id','title','image','author'])->whereIn('id',$book_ids_arr)->state()->undeleted()->stock()->inRandomOrder()->limit(6)->get();
                }
                else
                {
                    $books = Book::query()->select(['id','title','image','author'])->whereIn('id',$book_ids_arr)->state()->undeleted()->inRandomOrder()->limit(6)->get();
                }
            }
        }

        $items = [];
        if(!empty($books))
        {
            foreach ($books as $v)
            {
                $items[] = [
                    'id'     => $v->id,
                    'title'  => $v->title,
                    'image'  => $v->image,
                    'author' => $v->author,
                ];
            }
        }

        $data = [
            "items" => $items
        ];

        return $this->res([
            'data' => $data
        ]);
    }

    private function _check_info($book_id)
    {
        $bookModel = new Book();
        return $bookModel->getBookInfo($book_id);
    }

    public function comments(Request $request)
    {
        $book_id   = intval($request->get('book_id'));
        $page      = intval($request->get('page',1));
        $page      = max($page ,1);
        $page_size = intval($request->get('page_size',10));

        $builder = BookComment::query()->where('book_id',$book_id)->where('deleted',0)->where('state',1)->orderBy('id','desc');
        $columns = ['id','book_id','user_nickname','user_avatar','user_id','content'];
        $list = $builder->orderBy('id','desc')->paginate($page_size,$columns,'page',$page);
        $items = [];
        if(!empty($list->items()))
        {
            foreach ($list->items() as $item)
            {
                $items[] = [
                    'id'            => $item->id,
                    'book_id'       => $item->book_id,
                    'user_nickname' => $item->user_nickname,
                    'user_avatar'   => $item->user_avatar,
                    'user_id'       => $item->user_id,
                    'content'       => $item->content,

                ];
            }
        }

        $data = [
            '_meta' => [
                'total_count'  => $list->total(),
                'page_count'   => $list->lastPage(),
                'current_page' => $list->currentPage(),
                'per_page'     => $list->perPage()
            ],
            "items" => $items
        ];

        return $this->res([
            'data' => $data
        ]);
    }

    public function author(Request $request)
    {
        $book_id = intval($request->get('book_id'));
        if(empty($book_id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $book = $this->_check_info($book_id);
        if(empty($book))
        {
            return $this->err(1,200,'','图书不存在');
        }

        //dd($book);
        $columns = ['*'];

        $author_name = preg_replace('/\[(.*?)\]/', '', $book['author']);
        $like = '%'.trim($author_name).'%';
        //echo $like;die;
        $items = Book::query()->select($columns)->where('author','like',$like)->state()->undeleted()->orderBy('total_stock','desc')->get();

        $books = [];
        if($items->isNotEmpty())
        {
            foreach ($items as $item)
            {
                $stock_arr = [];
                $discount_arr = [];
                $price_arr = [];

                $discount_arr2 = [];
                $price_arr2 = [];
                if($item->new_book_stock > 0)
                {
                    $stock_arr[] = $item->new_book_stock;
                    $discount_arr2[] = $item->new_book_discount;
                    $price_arr2[] = $item->new_book_price;
                }
                else
                {
                    $discount_arr[] = $item->new_book_discount;
                    $price_arr[] = $item->new_book_price;
                }

                if($item->good_book_stock > 0)
                {
                    $stock_arr[] = $item->good_book_stock;
                    $discount_arr2[] = $item->good_book_discount;
                    $price_arr2[] = $item->good_book_price;
                }
                else
                {
                    $discount_arr[] = $item->good_book_discount;
                    $price_arr[] = $item->good_book_price;
                }

                if($item->medium_book_stock > 0)
                {
                    $stock_arr[] = $item->medium_book_stock;
                    $discount_arr2[] = $item->medium_book_discount;
                    $price_arr2[] = $item->medium_book_price;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount;
                    $price_arr[] = $item->medium_book_price;
                }

                if($item->medium_book_stock2 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock2;
                    $discount_arr2[] = $item->medium_book_discount2;
                    $price_arr2[] = $item->medium_book_price2;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount2;
                    $price_arr[] = $item->medium_book_price2;
                }

                if($item->medium_book_stock3 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock3;
                    $discount_arr2[] = $item->medium_book_discount3;
                    $price_arr2[] = $item->medium_book_price3;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount3;
                    $price_arr[] = $item->medium_book_price3;
                }

                if($item->medium_book_stock4 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock4;
                    $discount_arr2[] = $item->medium_book_discount4;
                    $price_arr2[] = $item->medium_book_price4;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount4;
                    $price_arr[] = $item->medium_book_price4;
                }

                if($item->medium_book_stock5 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock5;
                    $discount_arr2[] = $item->medium_book_discount5;
                    $price_arr2[] = $item->medium_book_price5;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount5;
                    $price_arr[] = $item->medium_book_price5;
                }

                if($item->medium_book_stock6 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock6;
                    $discount_arr2[] = $item->medium_book_discount6;
                    $price_arr2[] = $item->medium_book_price6;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount6;
                    $price_arr[] = $item->medium_book_price6;
                }

                $min_discount = 0;
                $min_price = 0.0;
                $is_more = 0;
                if(count($stock_arr) > 1)
                {
                    $is_more = 1;
                }

                if(count($stock_arr) == 0)
                {
                    if(!empty($discount_arr))
                    {
                        $min_discount = min($discount_arr);
                    }
                    if(!empty($price_arr))
                    {
                        $min_price = min($price_arr);
                    }
                }
                else
                {
                    if(!empty($discount_arr2))
                    {
                        $min_discount = min($discount_arr2);
                    }
                    if(!empty($price_arr2))
                    {
                        $min_price = min($price_arr2);
                    }
                }

                $books[] = [
                    'id'            => $item->id,
                    'title'         => $item->title,
                    'author'        => $item->author,
                    'score'         => $item->score,
                    'image'         => $item->image,
                    'old_price'     => $item->old_price,
                    'is_more'       => $is_more,
                    'min_discount'  => $min_discount,
                    'min_price'     => $min_price,
                    'total_stock'   => $item->total_stock,
                ];
            }
        }

        $result['author']       = $book['author'];
        $result['author_intro'] = $book['author_intro'];
        $result['books']        = $books;
        return $this->ok($result);
    }

    /**
     * 设置到货提醒和取消设置
     */
    public function remind(Request $request)
    {
        $book_id = intval($request->get('book_id'));
        $type    = intval($request->get('type',1)); //1设置 2取消设置


        if(empty($book_id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $book = $this->_check_info($book_id);
        if(empty($book))
        {
            return $this->err(1,200,'','图书不存在');
        }


        $book_info = Book::query()->select(['id','total_stock','remind_users_count'])->where('id',$book_id)->first();
        $row = BookStockRemindUser::query()->where('user_id',$this->user_id)->where('book_id',$book_id)->first();

        if($type == 1)
        {
            if($book_info['total_stock'] > 0)
            {
                return $this->err(1,200,'','操作失败');
            }

            //设置
            //判断用户是否已经关注了公众号
            $user = User::query()->select(['id','unionid'])->where('id',$this->user_id)->first();
            $wechat_user = \DB::table('wechat_users')->where('unionid',$user->unionid)->first();
            if(empty($wechat_user))
            {
                return $this->err(2,200,'','你还未开启消息提醒功能');
            }


            //判断是否已经设置了到货提醒
            if(!empty($row))
            {
                return $this->err(1,200,'','已经设置到货提醒');
            }


            $data = [
                'user_id'    => $this->user_id,
                'book_id'    => $book_id,
                'created_at' => $this->time,
            ];
            BookStockRemindUser::create($data);

            $book_info->increment('remind_users_count');
            return $this->ok([]);
        }
        else
        {
            //取消设置
            if(empty($row))
            {
                return $this->err(1,200,'','操作失败');
            }

            $row->delete();
            $book_info->decrement('remind_users_count');
            return $this->ok([]);
        }
    }
}
